<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Category;
use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class CategoryController extends CommonController
{
    //get 方法 admin/category 路由
    public function index()
    {
        $cate=new Category;
        $data=$cate->tree();
        return view('admin.category.index')->with('data',$data);
    }

    public function changeOrder()
    {
        $input=Input::all();
        $cate=Category::find($input['cate_id']);
        $cate->cate_order=$input['cate_order'];
        $res=$cate->update();
        if($res){
            $data=[
                'status'=>0,
                'msg'=>'分类排序更新成功',
            ];
        }else{
            $data=[
                'status'=>1,
                'msg'=>'分类排序更新失败',
            ];
        }
        return $data;
    }

    //get 方法 admin/category/create 路由  添加分类
    public function create()
    {
        $data=Category::where('cate_pid',0)->get();

        return  view('admin.category.add',compact('data'));
    }

    //post方法 admin/category 路由 添加分类  提交方法
    public function store()
    {         //打印除token值以外的数据
        if($input=Input::except('_token')) {

            $rules = array(
                'cate_name' => 'required ',
            );
            $mes=array(
                'cate_name.required'=>'分类名称不能为空',
            );
            $validator = Validator::make($input, $rules, $mes);

            if ($validator->passes()) {
                    $res=Category::create($input);
                    if($res){
                        return redirect('admin/category');
                    }else{
                           return back()->with('errors','数据填充失败，请稍后重试！');
                    }
            } else {
                return back()->withErrors($validator);
            }
        }
    }



    //get 方法  admin/category/{category}/edit 路由文章编辑
    public function edit($cate_id)
    {
        $filed=Category::find($cate_id);
        $data=Category::where('cate_pid',0)->get();
        return view('admin.category.edit',compact('filed','data'));
    }

    //PUT 方法 admin/category/{category} 路由
    public function update($cate_id)
    {
        $input=Input::except('_token','_method');
        $res=Category::where('cate_id',$cate_id)->update($input);

        if($res){
            return redirect('admin/category');
        }else{
            return back()->with('errors','数据更新失败，请稍后重试！');
        }

    }

    public function destroy($cate_id)
    {
        $re = Category::where('cate_id',$cate_id)->delete();
        Category::where('cate_pid',$cate_id)->update(['cate_pid'=>0]);
        if($re){
            $data = [
                'status' => 0,
                'msg' => '分类删除成功！',
            ];
        }else{
            $data = [
                'status' => 1,
                'msg' => '分类删除失败，请稍后重试！',
            ];
        }
        return $data;
    }

    //get 方法 admin/category/{category} 路由
    public function show()
    {

    }

}
